package View;

import java.awt.BorderLayout;
import java.awt.Dialog.ModalExclusionType;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import Controller.ControladorProveedor;
import Model.Supplier;

public class ProcesarSupplier extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private AbstractTableModel tmodelo;
	private List<Supplier> datos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProcesarSupplier frame = new ProcesarSupplier();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void actualizarModelo(JTable table) {
		ControladorProveedor cont = new ControladorProveedor();
		datos = cont.cargarTodos();
				
		tmodelo = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public int getColumnCount() {
				return 3;
			}

			@Override
			public String getColumnName(int col) {
				switch (col) {
				case 0:
					return "Codigo";
				case 1:
					return "Nombre";
				case 2:
					return "Descripcion";
				}
				return null;
			}

			@Override
			public int getRowCount() {
				return datos.size();
			}

			@Override
			public Object getValueAt(int row, int col) {
				Supplier obj = datos.get(row);
				switch (col) {
				case 0:
					return obj.getCed();
				case 1:
					return obj.getNombre();
				case 2:
					return obj.getDirec();
				}
				return null;
			}
		};

		table.setModel(tmodelo);
	}
	
	/**
	 * Create the frame.
	 */
	public ProcesarSupplier() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnMod = new JButton("Upd");
		
		btnMod.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int fila = table.getSelectedRow();
				ActualizarSupplier act = new ActualizarSupplier(datos.get(fila));		
				act.setVisible(true);			
			}
		});
		
		btnMod.setBounds(33, 216, 89, 23);
		contentPane.add(btnMod);

		JButton btnDel = new JButton("Del");
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ControladorProveedor controladorProveedor = new ControladorProveedor();
				int fila = table.getSelectedRow();
				controladorProveedor.Eliminar(datos.get(fila));
				actualizarModelo(table);
			}
		});
		btnDel.setBounds(132, 216, 89, 23);
		contentPane.add(btnDel);

		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ProcesarSupplier.this.dispose();
			}
		});
		btnClose.setBounds(229, 216, 89, 23);
		contentPane.add(btnClose);

		JScrollPane jScrollPane1 = new JScrollPane();
		contentPane.add(jScrollPane1);
		jScrollPane1.setBounds(24, 47, 383, 146);
		{
			table = new JTable();
			jScrollPane1.setViewportView(table);
			table.setBounds(49, 53, 288, 113);
		}
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GesionarProveedor pro = new GesionarProveedor();		
				pro.setVisible(true);
				pro.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
				actualizarModelo(table);
			}
		});
		btnAdd.setBounds(328, 216, 89, 23);
		contentPane.add(btnAdd);
		actualizarModelo(table);
	}
}
